﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.UserAd
{
    public partial class AdEdit : System.Web.UI.Page
    {
        private string adid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            adid = Request.QueryString["id"] + "";
            if (!Page.IsPostBack)
            {
                if (!CheckAd())
                {
                    Tools.AlertAndRedirect("该广告不允许您进行编辑", "MyAd.aspx");
                    return;
                }
                LoadContent();
            }
        }

        #region 加载内容
        private void LoadContent()
        {
            AdHelper ahelper = new AdHelper();
            Ad ainfo = new Ad();
            ainfo = ahelper.GetAd(adid);

            int adtype = ainfo.AdType;//1图片2文字
            
            lbAdPositionName.Text = ainfo.AdPositionName;
            txtAdName.Text = ainfo.AdName;
            txtLinkURL.Text = ainfo.LinkURL;
            rblImage.SelectedValue = adtype.ToString();

            if (adtype == 1)
            {
                imgAd.ImageUrl = "../AdImages/" + ainfo.ImageURL;
                txtScript.Value = ainfo.AdScript;
                imgAd.Visible = true;

                liimage1.Visible = true;
                liimage2.Visible = true;
                liimage3.Visible = true;
                liscript.Visible = false;
            }
            else if (adtype == 2)
            {
                imgAd.ImageUrl = "../AdImages/" + ainfo.ImageURL;
                txtScript.Value = ainfo.AdScript;
                imgAd.Visible = false;

                liimage1.Visible = false;
                liimage2.Visible = false;
                liimage3.Visible = false;
                liscript.Visible = true;
            }
        }
        #endregion

        #region 提交
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            AdHelper ahelper = new AdHelper();
            Ad ainfo = new Ad();
            ainfo = ahelper.GetAd(adid);

            int adtype = 1;
            adtype =Convert.ToInt32( rblImage.SelectedValue);

            ainfo.AdType = adtype;
            ainfo.AdName = txtAdName.Text;
            ainfo.LinkURL = txtLinkURL.Text;
            //系统账号添加不需要审批
            string systemaduserid = System.Configuration.ConfigurationSettings.AppSettings["systemaduserid"] + "";
            if (ainfo.UsersID == systemaduserid)
            {
                ainfo.Status = 1;
            }
            else
            {
                ainfo.Status = 0;
            }

            string imageurl = ainfo.ImageURL;
            string adscript = txtScript.Value.Trim();

            string newFileName = "";
            //圈子图片
            if (fuImage.HasFile)
            {
                string FileName = fuImage.FileName.ToString();
                string ExtenName = System.IO.Path.GetExtension(FileName);//获取扩展名
                string filename = FileName.Replace(ExtenName, "");
                newFileName = filename + System.Guid.NewGuid().ToString() + ExtenName;
                if (ExtenName == ".jpg" || ExtenName == ".gif" || ExtenName == ".png" || ExtenName == ".jpeg")
                {
                    string filePath = Server.MapPath("..\\AdImages\\") + newFileName;//合并两个路径为上传到服务器上的全路径,其中System.Web.HttpContext.Current.Request.MapPath("~/upload/")将指定的虚拟路径映射到物理路径
                    fuImage.SaveAs(filePath);
                }
                else
                {
                    Tools.Alert("图片格式不支持！");
                    return;
                }

                imageurl = newFileName;
            }
            if (adtype == 1)
            {
                if (string.IsNullOrEmpty(imageurl))
                {
                    Tools.Alert("请上传广告图片!");
                    return;
                }
            }
            else if (adtype == 2)
            {
                if (string.IsNullOrEmpty(adscript))
                {
                    Tools.Alert("请填写广告文字!");
                    return;
                }
            }

            ainfo.ImageURL = imageurl;
            ainfo.AdScript = adscript;

            ahelper.EditAd(ainfo);
            Tools.AlertAndRedirect("修改广告成功", "MyAd.aspx");
        }
        #endregion

        #region 判断该广告是否为该用户的
        private bool CheckAd()
        {
            Users uinfo = UserBase.GetCurrentUser();
            AdHelper ahelper = new AdHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "ID", adid);
            c.Add(CriteriaType.Equals, "UsersID", uinfo.ID);
            int count = ahelper.QueryAdCountBySearch(c);
            if (count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 返回
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("MyAd.aspx");
        }
        #endregion


        #region 类型改变
        protected void rblImage_SelectedIndexChanged(object sender, EventArgs e)
        {
            string adtype = rblImage.SelectedValue;
            if (adtype == "1")//图片
            {
                liimage1.Visible = true;
                liimage2.Visible = true;
                liimage3.Visible = true;
                liscript.Visible = false;
            }
            else if (adtype == "2")//文字
            {
                liscript.Visible = true;
                liimage1.Visible = false;
                liimage2.Visible = false;
                liimage3.Visible = false;
            }
        }
        #endregion
    }
}